package com.spring.sunflower.conversation

import android.content.Intent
import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.spring.sunflower.R
import com.spring.sunflower.base.BaseFragment
import com.spring.sunflower.bean.AnchorDetailBean
import com.spring.sunflower.bean.ChatGiftBean
import com.spring.sunflower.common.WalletActivity
import com.spring.sunflower.constant.Constant
import com.spring.sunflower.event.GiftSendSuccessEvent
import com.spring.sunflower.util.DoubleClickUtils
import org.greenrobot.eventbus.EventBus
import java.util.HashMap

/**
 * Created by Kevin on 2021/7/31<br/>
 *
 * Describe:<br/>
 */
class GiftFragment : BaseFragment<GiftPresenter>(), ConversationContract.GiftView {
    private var recyclerView: RecyclerView? = null
    private var mAdapter: GiftAdapter? = null
    private var data: MutableList<ChatGiftBean.Data> = ArrayList()
    private var clickPos = 0
    private var tvDonate: TextView? = null
    private var tvCharge: TextView? = null
    private var tvMoney: TextView? = null
    private var mUserID: String = ""
    private var mOtherID: String = ""
    override fun setLayoutResId(): Int = R.layout.fragment_gift

    override fun initView() {
        recyclerView = mView.findViewById(R.id.rvRecyclerView)
        tvDonate = mView.findViewById(R.id.tvDonate)
        tvCharge = mView.findViewById(R.id.tvCharge)
        tvMoney = mView.findViewById(R.id.tvMoney)
        mUserID = getField(Constant.KEY_USERID, "")
        mOtherID=getField(Constant.KEY_USERID_CHATTER, "")
        val gridLayoutManager = GridLayoutManager(mActivity, 4)
        recyclerView?.layoutManager = gridLayoutManager
        mAdapter = GiftAdapter(R.layout.adapter_chat_gift, data)
        recyclerView?.adapter = mAdapter
        mPresenter.loadAllGift()
        mPresenter.getDetail(mUserID)
        mAdapter?.setOnItemClickListener(object : OnItemClickListener {
            override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
                clickPos = position
                for (index in data.indices) {
                    data[index].checked = index == position
                }
                mAdapter?.notifyDataSetChanged()

            }

        })
    }

    override fun initData() {
    }

    override fun initListener() {
        tvDonate?.setOnClickListener {
            if (DoubleClickUtils.isFastLongClick()) {
                showToast("请不要频繁操作")
                return@setOnClickListener
            }
            val map: MutableMap<String, String> = HashMap()
            map["MID"] = mUserID
            map["anchorId"] = mOtherID
            map["giftId"] = data[clickPos].id.toString()
            map["number"] = "1"
            map["roomId"] = "0"
            map["type"] = "1"
            mPresenter.sendGift(map, clickPos)
        }
        tvCharge?.setOnClickListener {
            var intent = Intent(mActivity, WalletActivity::class.java)
            startActivity(intent)
        }
    }

    override fun loadData() {
    }

    override fun setPresenter(): GiftPresenter = GiftPresenter(this)
    override fun showGiftList(data: List<ChatGiftBean.Data>) {
        if (data.isNotEmpty()) {
            data[0].checked = true
        }
        mAdapter?.addData(data)
    }

    override fun showAnchorData(data: AnchorDetailBean.DatasBean?) {
        tvMoney?.text = if (data == null) "0" else data.money
        saveField(Constant.KEY_MONEY, data!!.money)
    }

    override fun sendGiftSuccess(money: String,pos:Int) {
        tvMoney?.text = money
        saveField(Constant.KEY_MONEY, money)
        val giftSendSuccessEvent = GiftSendSuccessEvent()
        val d = data[pos]
        giftSendSuccessEvent.giftPath = d.imagePath
        giftSendSuccessEvent.giftEffect = d.effects
        giftSendSuccessEvent.giftName = d.name
        giftSendSuccessEvent.giftNum = "1"
        EventBus.getDefault().post(giftSendSuccessEvent)
    }

    override fun sendGiftFailed(msg: String) {
        showToast("发送失败$msg")
    }
}